package com.springbootblog.service.serviceImpl.fontend;

import com.springbootblog.controller.fontend.IndexController;
import com.springbootblog.dao.ChatRecordDao;
import com.springbootblog.dao.UserDao;
import com.springbootblog.pojo.ChatRecord;
import com.springbootblog.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springbootblog.service.fontend.ChatService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChatServiceImpl implements ChatService
{
    /**
     * 自动装载（dao接口注入）
     */
    @Autowired
    private ChatRecordDao chatDao;

    /**
     * 自动装载（dao接口注入）
     */
    @Autowired
    private UserDao userDao;

    /**
     * 定义一个静态常量日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    /**
     *
     * @param to_user_id   接受消息用户id
     * @param from_user_id 发送消息用户id
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getChatRecord(String to_user_id, String from_user_id, HttpServletRequest request) throws Exception
    {
        // 将未读消息改成已读
        chatDao.updateChatRecordStatus(from_user_id,to_user_id);
        // 查聊天记录列表
        List<ChatRecord> list = chatDao.getChatRecordList(from_user_id,to_user_id);
        for (int i = 0; i < list.size(); i++)
        {
            list.get(i).setClasss("left");
            if(list.get(i).getFrom_user_id().toString().equals(from_user_id))
            {
                list.get(i).setClasss("right");
            }
            User userinfo = userDao.getUserInfoById(list.get(i).getFrom_user_id().toString());
            if(userinfo.getFigureurl_qq().equals(""))
            {
                list.get(i).setUserlogo(userinfo.getFigureurl_wx());
            }
            else
            {
                list.get(i).setUserlogo(userinfo.getFigureurl_qq());
            }
        }
        Map<String,Object> result = new HashMap<>(2);
        result.put("code", 1);
        result.put("list", list);
        return result;
    }
}
